package vip.liux.front.domain.models.payment;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Getter;
import org.springframework.data.relational.core.mapping.Table;
import vip.liux.core.objectExtending.ExtraProperty;
import vip.liux.core.objectExtending.IHasExtraProperties;
import vip.liux.domain.converts.jpa.ExtraPropertyConverter;
import vip.liux.domain.entities.TEntity;

import java.math.BigDecimal;
import java.util.Optional;


@jakarta.persistence.Entity
@Table("payment_request_product")
public class PaymentRequestProduct extends TEntity<PaymentRequestProductId> implements IHasExtraProperties {
    @ManyToOne
    @JoinColumn(name = "payment_request_id", insertable = false, updatable = false)
    private PaymentRequest paymentRequest;

    @Getter
    private String name;

    @Getter
    private BigDecimal unitPrice;

    @Getter
    private int count;

    @Getter
    private BigDecimal totalPrice;

    @Convert(converter = ExtraPropertyConverter.class)
    private ExtraProperty extraProperties;

    // Default constructor
    protected PaymentRequestProduct() {
        this.extraProperties = new ExtraProperty();
    }

    // Parameterized constructor
    public PaymentRequestProduct(String paymentRequestId, String code, String name, BigDecimal unitPrice, Integer count, BigDecimal totalPrice) {
        this.setId(new PaymentRequestProductId(paymentRequestId, code));
        this.name = name;
        this.unitPrice = unitPrice;
        this.count = count;
        this.totalPrice = Optional.ofNullable(totalPrice).orElse(unitPrice.multiply(BigDecimal.valueOf(count)));
        this.extraProperties = new ExtraProperty();
    }

    @Override
    public ExtraProperty getExtraProperties() {
        return extraProperties;
    }

    protected void setExtraProperties(ExtraProperty extraProperties) {
        this.extraProperties = extraProperties;
    }

    @JsonIgnore
    @Transient
    @Override
    public boolean isNew() {
        return getId() == null;
    }

    public String getCode() {
        if (getId() == null) {
            return null;
        }
        return getId().getCode();
    }

    public String getPaymentRequestId() {
        if (getId() == null) {
            return null;
        }
        return getId().getPaymentRequestId();
    }
}